﻿using System;
using RayDen.Library.Core;
using RayDen.Library.Core.Primitives;
using RayDen.RayEngine.Core.Types;

namespace RayDen.RayEngine.Engines.MCMC
{
    public class ContributionMap
    {
        public class PixelContribution
        {
            public Sample Sample;
            public RgbSpectrum Radiance;
            public float Weight;
            public int Passes;
        }

        private readonly PixelContribution[] Data;
        private int width, height;

        public PixelContribution this[int x, int y]
        {
            get { return Data[x + y * width]; }
        }

        public PixelContribution this[Sample s]
        {
            get
            {
                var x = Math.Min((int)(s.imageX + 0.5f), width - 1);
                var y = Math.Min((int)(s.imageY + 0.5f), height - 1);

                return Data[x + y * width];
            }
        }


        public ContributionMap(int width, int height)
        {
            this.width = width;
            this.height = height;
            Data = new PixelContribution[width * height];
            for (int i = 0; i < width * height; i++)
            {
                Data[i] = new PixelContribution();
            }
        }

        private FastRandom rnd = new FastRandom();

        public float Rndf()
        {
            return rnd.NextFloat();
        }
    }
}
